package scala.for_the.impatient

import scala.collection.immutable.HashMap

/**
  * Created by shichen on 2017/5/24.
  */
object ch04_tuple_and_map {

  ////////////////////// map
  def t_map(): Unit = {
    val scores = Map("a" -> 1,"b" -> 2, "c" -> 3)//不可变的Map
    //可变Map
    val m = scala.collection.mutable.Map("a" -> 1,"b" -> 2, "c" -> 3)
    //empty map
    val m2 = new HashMap[String,Int]()
    val a = scores("a")
    val b = scores.getOrElse("b",0)
    val d = scores.getOrElse("d",0)
    println((a,b,d))
    //添加新成员
    m("d") = 4
    m += ("e" -> 5, "f" -> 6)
    println(m) //乱序打印

    //不可改变不可变的map，但是可以由它创建新的map
    val scores2 = scores - "a"
    println(scores2)

    //遍历
    for((k,v) <- m) {
      for (k <- m.keySet) {
        for (v <- m.values) {}
      }
    }
  }

  /////////////////////// tuple
  def t_tuple(): Unit = {
    val t = (1,3.14,"cs")
    println(t)

    //使用模式匹配获取元组的组元
    val (first,second,_) = t
    println(first)
  }
}
